package com.zeta.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zeta.auth.dao.UserInfoMapper;
import com.zeta.auth.model.entity.UserInfo;
import com.zeta.auth.service.IUserInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.exceptions.TooManyResultsException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * 用户 服务实现类
 *
 * @author gcc
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements IUserInfoService {

    private final BCryptPasswordEncoder passwordEncoder;

    /**
     * 根据账号查询用户
     *
     * @param account String 账号
     * @return UserInfo
     */
    @Override
    public UserInfo getByAccount(String account) {
        UserInfo info = null;
        try {
            // 根据账号查询用户
            info = getOne(new LambdaQueryWrapper<UserInfo>()
                    .eq(UserInfo::getAccount, account)
            );
        } catch (TooManyResultsException e) {
            log.error("根据账号查询到多个用户，异常描述：{}", e.getMessage());
        }
        return info;
    }

    /**
     * 比较密码
     *
     * @param inputPwd String 用户输入的密码
     * @param dbPwd    String    用户数据库中的密码
     * @return Boolean
     */
    @Override
    public Boolean comparePassword(String inputPwd, String dbPwd) {
        return passwordEncoder.matches(inputPwd, dbPwd);
    }
}
